package cn.sam.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class HelloListener implements HttpSessionListener {
    int c = 0;
    //当Session创建的时候就会来这里
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        //表示当前人数
        httpSessionEvent.getSession().getServletContext().setAttribute("c",++c);
        System.out.println(c);
    }
    //当session销毁的时候会来这里
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        //当有人下线了
        httpSessionEvent.getSession().getServletContext().setAttribute("c",--c);
        System.out.println(c);
    }
}
